#region File Description
//-----------------------------------------------------------------------------
// MainMenuScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Media;
using System;
#endregion

namespace IcyTowerMobile
{
	/// <summary>
	/// The main menu screen is the first thing displayed when the game starts up.
	/// </summary>
	class MainMenuScreen : MenuScreen
	{

		private bool userSoundChecked;

		#region Initialization

		/// <summary>
		/// Constructor fills in the menu contents.
		/// </summary>
		public MainMenuScreen()
			: base("")
		{
			// Create our menu entries.
			MenuEntry playGameMenuEntry = new MenuEntry("Play Game");
			MenuEntry optionsMenuEntry = new MenuEntry("Options");
			MenuEntry highscoresMenuEntry = new MenuEntry("Highscores");
			MenuEntry howToMenuEntry = new MenuEntry("How To");
			MenuEntry aboutMenuEntry = new MenuEntry("About");

			// Hook up menu event handlers.
			playGameMenuEntry.Selected += PlayGameMenuEntrySelected;
			optionsMenuEntry.Selected += OptionsMenuEntrySelected;
			highscoresMenuEntry.Selected += HighscoresMenuEntrySelected;
			howToMenuEntry.Selected += HowToMenuEntrySelected;
			aboutMenuEntry.Selected += AboutMenuEntrySelected;

			// Add entries to the menu.
			MenuEntries.Add(playGameMenuEntry);
			MenuEntries.Add(optionsMenuEntry);
			MenuEntries.Add(highscoresMenuEntry);
			MenuEntries.Add(howToMenuEntry);
			MenuEntries.Add(aboutMenuEntry);
		}

		/// <summary>
		/// Starts the main menu background music.
		/// </summary>
		private void startBackgroundMusic()
		{
			if (IsolatedStorageManager.GetInstance().IsSoundEnabled())
			{
				FrameworkDispatcher.Update();
				if (MediaPlayer.GameHasControl)
				{
					// sounds are enabled and game has sound control, just start playing sound
					playSound();
				}
				else
				{
					// game has not sound control, ask user if game can play sounds
					MessageBoxScreen messageBox = new MessageBoxScreen("Is it ok to stop\ncurrently playing music\nand play our game music?", "Yes", "No");
					messageBox.Accepted += MusicMessageBoxAccepted;
					messageBox.Cancelled += MusicMessageBoxCancelled;
					ScreenManager.AddScreen(messageBox, null);
				}
			}
		}

		/// <summary>
		/// Plays the background sound.
		/// </summary>
		private void playSound()
		{
			MediaPlayer.Stop();
			MediaPlayer.IsRepeating = true;
			MediaPlayer.Volume = 0.3f;
			MediaPlayer.Play(ScreenManager.MenuSong);
		}

		#endregion

		#region Handle Input

		/// <summary>
		/// Event handler which is called when the music message box has been confirmed.
		/// </summary>
		void MusicMessageBoxAccepted(object sender, PlayerIndexEventArgs e)
		{
			// user wants the game to play music, so play it
			playSound();
		}

		/// <summary>
		/// Event handler which is called when the music message box has been declined.
		/// </summary>
		void MusicMessageBoxCancelled(object sender, PlayerIndexEventArgs e)
		{
			// do nothing here
		}

		/// <summary>
		/// Event handler for when the Play Game menu entry is selected.
		/// </summary>
		void PlayGameMenuEntrySelected(object sender, PlayerIndexEventArgs e)
		{
			if (MediaPlayer.GameHasControl)
			{
				// stop main menu background music
				MediaPlayer.Stop();
			}
			// start gameplay screen
			LoadingScreen.Load(ScreenManager, true, e.PlayerIndex,
							   new GameplayScreen());
		}


		/// <summary>
		/// Event handler for when the Options menu entry is selected.
		/// </summary>
		void OptionsMenuEntrySelected(object sender, PlayerIndexEventArgs e)
		{
			ScreenManager.AddScreen(new OptionsMenuScreen(), e.PlayerIndex);
		}

		/// <summary>
		/// Event handler for when the Highscores menu entry is selected.
		/// </summary>
		void HighscoresMenuEntrySelected(object sender, PlayerIndexEventArgs e)
		{
			ScreenManager.AddScreen(new HighscoresScreen(), e.PlayerIndex);
		}

		/// <summary>
		/// Event handler for when the how to menu entry is selected.
		/// </summary>
		void HowToMenuEntrySelected(object sender, PlayerIndexEventArgs e)
		{
			// show message box with how to instructions
			MessageBoxScreen messageBox = new MessageBoxScreen("Tilt your device left or\nright in order to move\nHarold. Press anywhere\non screen to make him\njump. The more speed you\ngive Harold horizontally\nvia tilting, the\nhigher he will jump.", "Got it!", null);
			messageBox.Accepted += HowToAccepted;
			ScreenManager.AddScreen(messageBox, e.PlayerIndex);
		}

		/// <summary>
		/// Event handler which is called when the how to message box is closed.
		/// </summary>
		void HowToAccepted(object sender, PlayerIndexEventArgs e)
		{
			// do nothing, just close how to dialog
		}

		/// <summary>
		/// Event handler for when the Highscores menu entry is selected.
		/// </summary>
		void AboutMenuEntrySelected(object sender, PlayerIndexEventArgs e)
		{
			ScreenManager.AddScreen(new AboutScreen(), e.PlayerIndex);
		}

		/// <summary>
		/// When the user cancels the main menu, we exit the game.
		/// </summary>
		protected override void OnCancel(PlayerIndex playerIndex)
		{
			ScreenManager.Game.Exit();
		}

		public override void Update(GameTime gameTime, bool otherScreenHasFocus,
													   bool coveredByOtherScreen)
		{
			base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

			if (!userSoundChecked)
			{
				// start background music the very first time this method is called
				userSoundChecked = true;
				startBackgroundMusic();
			}
		}

		#endregion
	}
}
